package com.service.lx;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.lx.DiystatusDAO;
import com.entity.Diystatus;
import com.service.ServiceException;


@Service
@Transactional(rollbackFor = Exception.class)
public class DiystatusService {
	@Autowired
	private DiystatusDAO diystatusDAO;
	/**
	 * 添加/修改/逻辑删除
	 * @param diystatus
	 * @throws ServiceException
	 */
	public void addAndUpdateDiystatus(Diystatus diystatus) throws ServiceException{
		try {
			Diystatus diystatus2=diystatusDAO.findOne(diystatus.getDiystatusId());
			if(diystatus2==null){
				diystatus.setDiystatusIfdelete(new BigDecimal(1));
			}
			diystatus.setDiystatusIntodate(new Date());
			diystatusDAO.save(diystatus);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param diystatus
	 * @throws ServiceException
	 */
	public void deleteDiystatus(Long diystatusId) throws ServiceException{
		try {
			diystatusDAO.delete(diystatusId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 逻辑删除
	 * @param diystatusId
	 */
	public void UpdateUserIfdelete(Long diystatusId) {
		Diystatus diystatus = diystatusDAO.findOne(diystatusId);
		diystatus.setDiystatusIfdelete(new BigDecimal(0));
		addAndUpdateDiystatus(diystatus);
	}
	
	/**
	 * 批量删除
	 * @param userIds
	 */
	public void deletArrIfDelete(List<Long> diystatusIds) {
		if(diystatusIds != null) {
			for(Long diystatusId: diystatusIds) {
				Diystatus diystatus = diystatusDAO.findOne(diystatusId);
				diystatus.setDiystatusIfdelete(new BigDecimal(0));
				addAndUpdateDiystatus(diystatus);
			}
		}
	}
	/**
	 * 单条查询
	 * @param detectionId
	 * @return
	 */
	public Diystatus findOne(Long diystatus) {
		return diystatusDAO.findOne(diystatus);
	}
	
	/**
	 * 查询所有
	 * @param detectionId
	 * @return
	 */
	public List<Diystatus> findAll() {
		return diystatusDAO.findAllDiystatusIfdelete();
	}
	
	/**
	 * 查询已启用
	 * @param detectionId
	 * @return
	 */
	public List<Diystatus> findAllOk() {
		return diystatusDAO.findAllOk();
	}
}
